import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
import {
	Message
} from 'element-ui'

// 要instant
// 基准路径
var instance = axios.create({
	baseURL: "http://127.0.0.1:80"
})

// 集成

// 有四种方式提交 get post+kv post+File post+json

// get封装支持两种写法
// 请求路径url  /api/adm/clz/findById?id=100  $get("/api/adm/clz/findById?id=100")
// 请求路径url  /api/adm/clz/findById  $get("/api/adm/clz/findById",{id:100})
// 参数： 对象格式
export let $get = (url) => {
	//    可以带token [不建议这种写法]
	let token = window.localStorage.getItem("token");
	token = "Bearer "+token;
	let head = {
						"headers": {
							'Authorization': token
						}}
	return instance.get(url,head)
};

// post键值对
// 请求路径url ，obj是对象
// $postkv("/login",{username:'12111',password:'45'})
export let $postkv = (url, obj) => {
 let token = window.localStorage.getItem("token")
 token = "Bearer " + token;
 console.log("token = " + token);
 let head = {"headers": {'Authorization': token,
 'Content-Type': 'application/x-www-form-urlencoded;charset=utf8'
 }}
 let kv = qs.stringify(obj) // 转成键值对
 return instance.post(url,kv,head)
};


// post文件
// 请求路径url ，formdata是对象
// $postf("/api/user/updatePic",formdata)
export let $postf = (url, formdata) => {
	let token = window.localStorage.getItem("token")
	token = "Bearer " + token;
	let head = {
		"headers": {
			'Authorization': token,
			'Content-Type': 'multipart/form-data'
		}
	}
	return instance.post(url, formdata, head)
}

// post Json 对象或数组
// 请求路径url ，formdata是对象
export let $postj = (url, obj) => {
	let token = window.localStorage.getItem("token")
	token = "Bearer " + token;
	let head = {
		"headers": {
			'Authorization': token,
			'Content-Type': 'application/json;chartset=utf8'
		}
	}
	return instance.post(url, obj, head)
}
